import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * service 员工定义
 * @date: 2018-6-27


 */
import { filterNullValueObject, parseParameters } from 'utils/utils';
import request from 'utils/request';
import { CPAAS_IAM, CPAAS_PLATFORM } from 'utils/config'; // const CPAAS_PLATFORM = `/cpfm-16353`;

/**
 * 请求API前缀
 * @type {string}
 */

var prefix = "".concat(CPAAS_PLATFORM, "/v1");
/**
 * 查询员工信息
 * @async
 * @function searchEmployee
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!object} params.page - 分页参数
 * @param {?string} params.employeeNum - 员工编码
 * @param {?string} params.name - 员工姓名
 * @returns {object} fetch Promise
 */

export function searchEmployee(_x) {
  return _searchEmployee.apply(this, arguments);
}
/**
 * 添加员工信息
 * @async
 * @function addEmployee
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!array<object>} params.saveData - 新增员工信息列表
 * @returns {object} fetch Promise
 */

function _searchEmployee() {
  _searchEmployee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var param;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            param = parseParameters(params);
            return _context.abrupt("return", request("".concat(prefix, "/").concat(param.tenantId, "/employees"), {
              method: "GET",
              query: param
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _searchEmployee.apply(this, arguments);
}

export function addEmployee(_x2) {
  return _addEmployee.apply(this, arguments);
}
/**
 * 新增/更新员工信息
 * @async
 * @function saveEmployee
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!number} params.positionId - 主岗Id
 * @param {!object[]} params.saveData - 待保存数据对象列表
 * @param {?string} params.saveData[].employeeNum - 员工编码
 * @param {!string} params.saveData[].name - 员工姓名
 * @returns {object} fetch Promise
 */

function _addEmployee() {
  _addEmployee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employees"), {
              method: 'POST',
              query: {
                tenantId: params.tenantId,
                flexModelCode: 'CPFM.EMPLOYEE'
              },
              body: _toConsumableArray(params.saveData)
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _addEmployee.apply(this, arguments);
}

export function saveEmployee(_x3) {
  return _saveEmployee.apply(this, arguments);
}
/**
 * 获取员工已分配岗位信息(非分页)
 * @async
 * @function fetchPosition
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!object[]} params.employeeId - 员工Id
 * @returns {object} fetch Promise
 */

function _saveEmployee() {
  _saveEmployee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var tenantId, saveData, positionId, customizeUnitCode;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            tenantId = params.tenantId, saveData = params.saveData, positionId = params.positionId, customizeUnitCode = params.customizeUnitCode;
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(tenantId, "/employees/employee-position"), {
              method: "POST",
              query: {
                positionId: positionId,
                tenantId: tenantId,
                customizeUnitCode: customizeUnitCode
              },
              body: _objectSpread({}, saveData)
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _saveEmployee.apply(this, arguments);
}

export function fetchPosition(_x4) {
  return _fetchPosition.apply(this, arguments);
}
/**
 * 根据租户Id和员工Id，获取岗位信息(树形结构)
 * @async
 * @function searchPositionTree
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!string} params.employeeId - 员工Id
 * @returns {object} fetch Promise
 */

function _fetchPosition() {
  _fetchPosition = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employee-assigns"), {
              method: 'GET',
              query: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _fetchPosition.apply(this, arguments);
}

export function searchPositionTree(_x5) {
  return _searchPositionTree.apply(this, arguments);
}
/**
 * 批量更新员工岗位分配信息
 * @async
 * @function updatePosition
 * @param {!object} params
 * @param {!number} params.tenantId - 租户Id
 * @param {!string} params.employeeId - 员工Id
 * @param {!array<object>} params.positionList - 岗位信息列表
 * @returns {object} fetch Promise
 */

function _searchPositionTree() {
  _searchPositionTree = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/unit-aggregate/").concat(params.employeeId), {
              method: 'GET',
              query: filterNullValueObject(params)
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _searchPositionTree.apply(this, arguments);
}

export function updatePosition(_x6) {
  return _updatePosition.apply(this, arguments);
}
/**
 * 获取员工已分配的用户信息(非分页)
 * @async
 * @function fetchUser
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!string} params.employeeId - 员工Id
 * @returns {Object} fetch Promise
 */

function _updatePosition() {
  _updatePosition = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employee-assigns"), {
              method: 'POST',
              query: {
                employeeId: params.employeeId
              },
              body: _toConsumableArray(params.positionList)
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _updatePosition.apply(this, arguments);
}

export function fetchUser(_x7) {
  return _fetchUser.apply(this, arguments);
}
/**
 * 批量移除员工已分配的用户
 * @async
 * @function deleteUser
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!object[]} params.employeeUsers - 员工Id
 * @returns {Object} fetch Promise
 */

function _fetchUser() {
  _fetchUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employee-users"), {
              method: 'GET',
              query: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _fetchUser.apply(this, arguments);
}

export function deleteUser(_x8) {
  return _deleteUser.apply(this, arguments);
}
/**
 * 批量添加员工-分配的用户
 * @async
 * @function updateUser
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!object[]} params.employeeUsers - 员工Id
 * @returns {Object} fetch Promise
 */

function _deleteUser() {
  _deleteUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employee-users"), {
              method: 'DELETE',
              body: _toConsumableArray(params.data)
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _deleteUser.apply(this, arguments);
}

export function updateUser(_x9) {
  return _updateUser.apply(this, arguments);
}
/**
 * 查询用户信息
 * @async
 * @function searchUserList
 * @param {Object} params - 接口参数
 * @param {Number} params.tenantId - 租户ID
 * @returns {Object} fetch Promise
 */

function _updateUser() {
  _updateUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(prefix, "/").concat(params.tenantId, "/employee-users"), {
              method: 'POST',
              body: _toConsumableArray(params.data)
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _updateUser.apply(this, arguments);
}

export function searchUserList(_x10) {
  return _searchUserList.apply(this, arguments);
}
/**
 * 查询员工信息
 * @async
 * @function searchEmployeeById
 * @param {object} params - 请求参数
 * @param {!number} params.tenantId - 租户Id
 * @param {!object} params.page - 分页参数
 * @param {?string} params.employeeId - 员工编码
 * @returns {object} fetch Promise
 */

function _searchUserList() {
  _searchUserList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    var param;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            param = parseParameters(params);
            return _context10.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/").concat(param.tenantId, "/users/multi-tenant-list"), {
              method: 'GET',
              query: _objectSpread({
                excludeHasAssignedEmployeeFlag: true
              }, param)
            }));

          case 2:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _searchUserList.apply(this, arguments);
}

export function searchEmployeeById(_x11) {
  return _searchEmployeeById.apply(this, arguments);
}
/**
 * 处理接口获取的数据，提取每个节点的层次路径
 * @param {array} [collections = [] ] - 页面展示数据
 * @param {array} levelPath - 特定组织的层级路径
 * @returns {object} 节点树和层次路径组成的对象
 * @returns {Object} 节点路径对象
 */

function _searchEmployeeById() {
  _searchEmployeeById = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(params) {
    var param;
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            param = parseParameters(params);
            return _context11.abrupt("return", request("".concat(prefix, "/").concat(param.tenantId, "/employees/id"), {
              method: "GET",
              query: param
            }));

          case 2:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _searchEmployeeById.apply(this, arguments);
}

export function renderTreeData() {
  var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var levelPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var parent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var pathMap = levelPath;
  collections.map(function (item) {
    var temp = item;
    var parentPath = parent.typeWithId ? pathMap[parent.typeWithId] || [] : [];
    pathMap[temp.typeWithId] = [].concat(_toConsumableArray(parentPath), [temp.typeWithId]);

    if (item.children) {
      renderTreeData(temp.children, pathMap, item);
    }

    return pathMap;
  });
  return pathMap;
}